package lookie.richclient.state;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.springframework.core.io.AbstractResource;

public class StateLocationResource extends AbstractResource {
	
	private StateLocation stateLocation;
	
	private String namespace;
	
	private String name;

	public StateLocation getStateLocation() {
		return stateLocation;
	}

	public void setStateLocation(StateLocation stateLocation) {
		this.stateLocation = stateLocation;
	}

	public String getNamespace() {
		return namespace;
	}

	public void setNamespace(String namespace) {
		this.namespace = namespace;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return namespace + ":" + name;
	}

	@Override
	public File getFile() throws IOException {
		if (name == null) {
			return stateLocation.getDirectory(namespace);
		}
		return stateLocation.getFile(namespace, name);
	}

	public InputStream getInputStream() throws IOException {
		File file = stateLocation.getFile(namespace, name);
		return new FileInputStream(file);
	}

}
